﻿using HlElock.Api;
using HlElock.Api.Models;
using HlELock.Page.Views;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace HlELock.Page.ViewModels
{
 
        public class EditRoomViewModel : BindableBase
        {
            private readonly RoomApiClient _apiClient;
            private readonly RoomInfo _originalRoomInfo; // 用于更新列表时引用

            // 用于编辑的表单数据 (现在直接使用 EditRoomRequest)
            private EditRoomRequest _editRoomForm;
            public EditRoomRequest EditRoomForm
            {
                get => _editRoomForm;
                set => SetProperty(ref _editRoomForm, value);
            }

            // 更新命令
            public DelegateCommand UpdateRoomCommand { get; }

            public EditRoomViewModel(RoomApiClient apiClient, EditRoomRequest initialEditRequest, RoomInfo originalRoomInfo)
            {
                _apiClient = apiClient ?? throw new ArgumentNullException(nameof(apiClient));
                _originalRoomInfo = originalRoomInfo ?? throw new ArgumentNullException(nameof(originalRoomInfo));

                // 初始化表单数据
                _editRoomForm = initialEditRequest;

                // 初始化命令
                UpdateRoomCommand = new DelegateCommand(async () => await UpdateRoomAsync(), CanUpdate).ObservesProperty(() => EditRoomForm);
            }

            private async Task UpdateRoomAsync()
            {
                try
                {
                    bool success = await _apiClient.EditRoomAsync(EditRoomForm);
                    if (success)
                    {
                        // 更新原始 RoomInfo 对象的属性，以便主窗口列表能同步更新
                        _originalRoomInfo.FlType = EditRoomForm.FlType;
                        _originalRoomInfo.TjDate = EditRoomForm.TjDate;
                        _originalRoomInfo.ExtNum = EditRoomForm.ExtNum;
                        _originalRoomInfo.OperId = EditRoomForm.OperId;
                        _originalRoomInfo.Floor = EditRoomForm.Floor;
                        _originalRoomInfo.Build = EditRoomForm.Build;
                        _originalRoomInfo.Window = EditRoomForm.Window;
                        _originalRoomInfo.PrjCode = EditRoomForm.PrjCode;
                        _originalRoomInfo.RoomId = EditRoomForm.RoomId;
                        _originalRoomInfo.Face = EditRoomForm.Face;
                        _originalRoomInfo.PreId = EditRoomForm.PreId;
                        _originalRoomInfo.SufId = EditRoomForm.SufId;

                        MessageBox.Show("房间信息更新成功！", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
                        // 关闭窗口
                        Application.Current.Windows.OfType<EditRoomWindow>().FirstOrDefault()?.Close();
                    }
                    else
                    {
                        MessageBox.Show("更新房间失败，请检查网络或输入信息。", "失败", MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"更新房间时发生错误: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

            private bool CanUpdate()
            {
            return !string.IsNullOrWhiteSpace(EditRoomForm.RoomId) &&
                 !string.IsNullOrWhiteSpace(EditRoomForm.FlType) &&
                 !string.IsNullOrWhiteSpace(EditRoomForm.Floor) &&
                 !string.IsNullOrWhiteSpace(EditRoomForm.Build) &&
                 !string.IsNullOrWhiteSpace(EditRoomForm.TjDate) &&
                 !string.IsNullOrWhiteSpace(EditRoomForm.OperId);
        }
        }
    }
